//: net/mindview/util/Hex.java
package net.mindview.util;

import java.io.*;

public class Hex
{
    public static String format(byte[] data)
    {
        StringBuilder result = new StringBuilder();
        int n = 0;
        for (byte b : data)
        {
            if (n % 16 == 0)
                result.append(String.format("%05X: ", n));
            result.append(String.format("%02X ", b));
            n++;
            if (n % 16 == 0)
                result.append("\n");
        }
        result.append("\n");
        return result.toString();
    }
    
    public static void main(String[] args)
        throws Exception
    {
        if (args.length == 0)
            // Test by displaying this class file:
            System.out.println(format(BinaryFile.read("Hex.class")));
        else
            System.out.println(format(BinaryFile.read(new File(args[0]))));
    }
} /*
   * Output: (Sample) 00000: CA FE BA BE 00 00 00 31 00 52 0A 00 05 00 22 07 00010: 00 23 0A 00 02 00 22 08 00 24 07 00
   * 25 0A 00 26 00020: 00 27 0A 00 28 00 29 0A 00 02 00 2A 08 00 2B 0A 00030: 00 2C 00 2D 08 00 2E 0A 00 02 00 2F 09 00
   * 30 00 00040: 31 08 00 32 0A 00 33 00 34 0A 00 15 00 35 0A 00 00050: 36 00 37 07 00 38 0A 00 12 00 39 0A 00 33 00 3A
   * ...
   */// :~
